//
//  SSEmoticonCellTipView.swift
//  shanshanlol微博
//
//  Created by 叶竞泽 on 2017/2/14.
//  Copyright © 2017年 JingZe. All rights reserved.
//

import UIKit
import pop

/// 表情选择提示视图
class SSEmoticonCellTipView: UIImageView {
    
    /// 之前选择的表情
    private var preEmoticon: SSEmoticon?
    
    /// 提示视图的表情模型
    var emoticon: SSEmoticon? {
        
        didSet {
            
            // 判断表情是否变化
            if emoticon == preEmoticon {
                
                // 没有变化
                return
            } else {
                
                // 有变化
                print("设置表情...")
                preEmoticon = emoticon
                
                // 设置表情提示按钮图片
                tipButton.setTitle(emoticon?.emoji, for: .normal)
                tipButton.setImage(emoticon?.image, for: .normal)
                
                // 设置动画 - 弹力动画的时间是根据速度计算的,不能指定duration
                let pop = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
                pop?.fromValue = 30
                pop?.toValue = 20
                pop?.springBounciness = 15
                pop?.springSpeed = 20
                tipButton.pop_add(pop, forKey: nil)
            }
        }
    }
    
    // MARK: - 私有属性
    private lazy var tipButton = UIButton()

    init() {
        
        let bundle = SSEmoticonManager.shared.bundle
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
        
        super.init(image: image)
        
        // 设置按钮
        tipButton.frame = CGRect(x: 0, y: 0, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 32)
        tipButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: -7, bottom: 0, right: 0)
        // 添加按钮
        addSubview(tipButton)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
